#!/bin/bash
#
# The MIT License (MIT)
# Copyright (c) 2016 Howard Liu
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, 
# and/or sell copies of the Software, and to permit persons to whom the 
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# System Requirement:
# OS = Debian or Ubuntu or those derived from Debian
# Able to execute command
# A pre-configured mysql database.
# You can import ~/shadowsocks-py-mu/shadowsocks/shadowsocks.sql into your database.
# Or https://raw.githubusercontent.com/mengskysama/shadowsocks-py-mu/manyuser/shadowsocks/shadowsocks.sql
echo -ne "\e[32m"
echo ===========================================
echo Shadowsocks Manyuser Auto-installing Script
echo -- For RHEL/CentOS/Debian/Ubuntu
echo Howard Liu
echo Version 1.1
echo ===========================================
echo -ne "\e[m"
# Remove old files
killall python -s KILL
rm shadowsocks-py-mu -rf
sleep 2

if [ $UID -ne 0 ]; then
	echo -e "\e[31m"
	echo ===========================================
	echo PLEASE RUN THIS SCRIPT UNDER ROOT!
	echo ===========================================
	echo -e "\e[m"
	exit 1
fi

# Thanks Miny for this section of code
if [ -f /etc/redhat-release ];then
    OS='CentOS'
elif [ ! -z "`cat /etc/issue | grep bian`" ];then
    OS='Debian'
elif [ ! -z "`cat /etc/issue | grep Ubuntu`" ];then
    OS='Ubuntu'
else
    echo "OS is not supported, Please reinstall Debian, Ubuntu or CentOS/RHEL and retry!"
    exit 1
fi

echo -ne "\e[34m"
echo ===========================================
echo Installing Dependencies
echo ===========================================
echo -ne "\e[m"
if [ "$OS" == 'CentOS' ]; then
    yum install -y epel-release
    yum install -y sudo openssl-devel python-devel git-core make gcc python-pip sed
else
    apt-get -y update
    apt-get -y install openssl python-dev git make gcc python-pip sed\
    libglib2.0 libglib2.0-dev screen build-essential
fi
# Install python library dependencies of Shadowsocks
pip install --upgrade pip
# Install libsodium for chacha20 encryption
if [ ! -f /usr/local/lib/libsodium.so ];then
    wget https://download.libsodium.org/libsodium/releases/libsodium-1.0.11.tar.gz
    tar zxvf libsodium-1.0.11.tar.gz && cd libsodium-1.0.11
     ./configure && make -j2
    make check && make install
    ldconfig    
    cd ..
    rm -rf libsodium-1.0.11 libsodium-1.0.11.tar.gz
else
    echo Libsodium has been installed already, skipped...
fi

echo -ne "\e[34m"
echo ===========================================
echo Cloning Sources of SS Manyuser
echo SS-Python-Mu is maintained by fsgmhoward
echo ===========================================
echo -ne "\e[m"
cd ~
git clone https://github.com/fsgmhoward/shadowsocks-py-mu.git

cd ~/shadowsocks-py-mu/shadowsocks
cp config_example.py config.py
echo -ne "\e[34m"
echo ===========================================
echo Configuring SS Manyuser
echo ===========================================
echo -e "\e[33mPlease DO NOT skip any question by just pressing ENTER!"
echo -e "\e[33mDo you want to configure ss-panel API? (0 for no, 1 for yes):\e[m"
read isapi
if [ $isapi -eq 1 ];then
    sed -i "s/API_ENABLED = False/API_ENABLED = True/" config.py
    echo -e "\e[33mPlease enter the address of the API (eg. http://ss.example.com/mu):\e[m"
    read apiAddress
    echo -e "\e[33mPlease enter API Key (eg. 123456):\e[m"
    read apiKey
    echo -e "\e[33mPlease enter Node ID (eg. 1):\e[m"
    read nodeID
    # Escape slashes
    apiAddress=$(echo $apiAddress | sed 's/\//\\\//g')
    sed -i "s/http:\/\/domain\/mu/$apiAddress/" config.py
    sed -i "s/mupass/$apiKey/" config.py
    sed -i "s/NODE_ID = '1'/NODE_ID = '$nodeID'/" config.py
else
    pip install cymysql
    echo -e "Please enter your database address excluding the port (eg. database.example.com):\e[m"
    read dbaddress
    echo -e "\e[33mPlease enter your database port (eg. 3306):\e[m"
    read dbport
    echo -e "\e[33mPlease enter your database username (eg. root):\e[m"
    read dbuser
    echo -e "\e[33mPlease enter your database password (eg. 123456):\e[m"
    read dbpass
    echo -e "\e[33mPlease enter your database name (eg. shadowsocks):\e[m"
    read dbname
    sed -i "s/mengsky.net/$dbaddress/" config.py
    sed -i "s/3306/$dbport/" config.py
    sed -i "s/MYSQL_USER = 'root'/MYSQL_USER = '$dbuser'/" config.py
    sed -i "s/MYSQL_PASS = 'root'/MYSQL_PASS = '$dbpass'/" config.py
    sed -i "s/MYSQL_DB = 'shadowsocks'/MYSQL_DB = '$dbname'/" config.py
fi

echo -ne "\e[32m"
echo Shadowsocks is basically configured, but if
echo you want to further configure it, please edit
echo ~/shadowsocks-py-mu/shadowsocks/config.py
echo
echo -ne "\e[34m"
echo ===========================================
echo Optimizing Shadowsocks Server
echo ===========================================
echo -ne "\e[m"
# Optimizing Sysctl
wget --no-check-certificate https://git.ixnet.work/fsgmhoward/Bash-Scripts/raw/master/ss_sysctl
cat ss_sysctl >> /etc/sysctl.conf
sysctl -p
# Optimizing Limits
su -c "echo * soft nofile 51200 >> /etc/security/limits.conf"
su -c "echo * hard nofile 51200 >> /etc/security/limits.conf"

echo -ne "\e[34m"
echo ===========================================
echo Writing Starting Script
echo ===========================================
echo -ne "\e[m"
cd /usr/bin
echo '#!/bin/bash' > ss-start
echo echo -e \"\\e[34mKilling existing instances\\e[m\" >> ss-start
echo killall python -s KILL >> ss-start
# Wait for 2 seconds until resources are released
echo sleep 2 >> ss-start
echo echo -e \"\\e[34mStarting Shadowsocks\\e[m\" >> ss-start
echo cd ~/shadowsocks-py-mu/shadowsocks >> ss-start
echo screen -dmS SS python servers.py >> ss-start
echo echo -e \"\\e[32mStarted\\e[m\" >> ss-start
chmod +x ss-start

# Add to startup
cd /etc
sed -i '$i ulimit -n 51200' rc.local
sed -i '$i sudo ss-start' rc.local

echo -ne "\e[32m"
echo ===========================================
echo Installation Finished
echo You can simply type 'ss-start' to start
echo This command can also restart the SS server
echo ===========================================
echo -e "\e[m"